// packageF/crowd_activity/crowd_activity_OrderPay/crowd_activity_OrderPay.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    isclick: true,
    order_id: "",
    store_id: "",
    orderData: null,
    balance: 0,
    payParams: "",
    info_form: {},
    order_sn: "",
    money: "",
    buttons: [],
    pay_sn: "订单编号",
    order_pay_id: "",
    uid: "",
    pid: "",
    status: "",

    //消费红包
    showPacket: false,
    redirectData: {},
    cbHref: {},
    is_cps: false,
    senceKey: "",
    popupSpecs: false,
    payarr: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.options = options;
    wx.setNavigationBarTitle({
      title: '订单支付',
    })
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
    })
    this.getData();
  },
  onChange(e){
    this.setData({
      radio:e.detail
    })
  },
  onClick(e){
    this.setData({
      radio:e.currentTarget.dataset.item.value
    })
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.pre-activity-order.detail");
    app._getNetWork({
      url: urlStr,
      data: {
        order_sn: this.data.options.sn,
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            order_sn: res.data.order_sn,
            money: res.data.amount,
            buttons: res.data.buttons,
          })
          if (this.data.buttons[0]) {
            this.setData({
              radio: this.data.buttons[0].value
            })
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  refChange(e) {
    for (let key in this.data.buttons) {
      if (this.data.buttons[key].value == e) {
        return this.data.buttons[key];
      }
    }
  },
  payKeyboardClose(){
    this.setData({
      payarr:'',
      popupSpecs:false
    })
  },
  payKeyboardComplete(e) {
    console.log(e, 'dddddddddd')
    this.getVerifyPassword(e.detail)
  },
  getVerifyPassword(pwd){
    let urlStr = app.getNetAddresss("payment.password.check");
    urlStr += "&password=" + pwd;
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            payarr: pwd
          })
          this.confirm();
        } else {
          this.setData({
            payarr: ''
          })
          wx.showModal({
            title: '提示',
            content: res.msg,
            success(restext) {
              if (restext.confirm) {
                if (res.data.code == 2001) {
                  //商城支付密码设置未开启
                } else if (res.data.code == 2002) {
                  //用户未设置支付密码
                  //去设置密码
                  wx.navigateTo({
                    url: '/packageA/member/set_balance_password/set_balance_password'
                  });
                } else if (res.data.code == 2003) {
                  //支付密码错误
                }
              } else if (restext.cancel) {
                if (res.data.code == 2001) {
                  //商城支付密码设置未开启
                } else if (res.data.code == 2002) {
                  //用户未设置支付密码
                } else if (res.data.code == 2003) {
                  //支付密码错误
                }
              }
            }
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  async confirm() {
    if (this.data.radio && !this.data.payarr) {
      let keyOrr = await this.refChange(this.data.radio);
      if (keyOrr.need_password) {
        //需要密码
        this.setData({
          popupSpecs: true
        })
        return
      }
    }
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.activity-order.pay");
    app._getNetWork({
      url: urlStr,
      data: {
        order_sn: this.data.options.sn,
        amount: this.data.money,
        pay_type_id: this.data.radio
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          if (res.data.config) {
            this.WXPay_other(res.data.config)
          } else {
            this.data.isclick = false;
            app.tips(res.msg)
            wx.showToast({
              title: res.msg,
              duration:1000,
              icon:'none',
              success:(resdata)=>{
                setTimeout(() => {
                  wx.redirectTo({
                    url: '/packageF/crowd_activity/index/index',
                  })
                }, 1000);
              }
            })
           
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  WXPay_other(payParams) {
    wx.requestPayment({
      timeStamp: payParams.timestamp,
      nonceStr: payParams.nonceStr,
      package: payParams.package,
      signType: payParams.signType ? payParams.signType : "MD5",
      paySign: payParams.paySign,
      success: (res) => {
        wx.showModal({
          title: "提示",
          content: "支付成功",
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/packageF/crowd_activity/index/index',
              })
            }
          },
        });
      },
      fail: (res) => {
        console.log(res);
        wx.showToast({
          title: "支付失败请稍后重试",
          icon: "none",
          duration: 2000,
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    let str = `/packageF/crowd_activity/crowd_activity_OrderPay/crowd_activity_OrderPay?sn=${this.data.options.sn}`
    if(mid){
      str +=`&mid=${mid}`
    }
    return {
      title: '订单支付',
      path: str,
    }
  },
  onShareTimeline(){
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    let str = `/packageF/crowd_activity/crowd_activity_OrderPay/crowd_activity_OrderPay?sn=${this.data.options.sn}`
    if(mid){
      str +=`&mid=${mid}`
    }
    return {
      title: '订单支付',
      query: str,
    }
  }
})